/**
 * @file ml307.h
 * @brief This is the header file for ml307.c, which is the porting file for the AT command
 * module.
 * @version 1.0
 */

#ifndef ML307_H
#define ML307_H
#include <ctype.h>  // 添加 isdigit() 所需的头文件
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "t_at.h"
#include "t_at_urc_etgp.h"
#ifdef __cplusplus
extern "C" {
#endif

#define ML_FALSE 0
#define ML_TRUE 1

/**
 * @brief Enumeration representing the possible states of the ML307 device.
 */
typedef enum {
    ml_online = 1, /**< The device is online. */
    ml_power_on,   /**< The device is powered on. */
    ml_power_down, /**< The device is powered down. */
} ml_dev_state;
/**
 * @brief Structure representing network information for ML307 module.
 */
typedef struct {
    char ip_4g[16];     // IP address of the 4G module.
    char net_name[6];   // China network name (MNC and MCC combination).
    char iccid[25];     // ICCID of the SIM card.
    char imsi[16];      // IMSI of the SIM card.
    uint8_t db_singal;  // Signal strength.
} ml_net_info;

typedef enum {
    ml_error = -1,
    ml_powr_open_ok,
    ml_powr_open_fail,
    ml_init_ok,
    ml_send_ok,
    ml_send_fail,
} ml307_error_t;

typedef enum {
    mqt_typedef_id = 0,//ID错误
    mqt_ok,          //ID正确
    mqt_param_null, //参数为空
} mqttc_error_t;

typedef struct {
    char subtopic[50];
} mqtt_mux_topic;

// 最大支持3个主体订阅
typedef struct {
    uint8_t mqttc_link:3;        //连接状态 0:未连接 1:连接中 2:已连接
    uint8_t ml307_mqtt_id;       // MQtt客户端ID号
    uint8_t ssl : 1;             // 是否启用加密连接 0:不启用 1:启用
    uint8_t topic_mux : 1;       // 是否启用多主题订阅 0:不启用 1:启用
    char txt_name[50];           // name
    char client_id[50];          // client_id
    char user_name[50];          // user_name
    char pass_word[50];          // pass_word
    char host[50];               // host
    int port;                    // port
    mqtt_mux_topic SubTopic[3];  // 订阅主题
    char PubTopic[50];
    int Qos;  // qos
} ml_mqttc_cfg;

typedef struct {
    uint8_t ml307_register_en;  // 是否启用自动注册 0:不启用 1:启用
    uint8_t ml307_register_sta; // 注册状态 0:未注册,2已注册
    uint16_t sp_time;         // AT指令间隔时间(单位:秒)
} boker_regiser_sta;

typedef struct {
    ml_net_info ml307_net_info_t;
    ml_dev_state ml307_status;
    ml_mqttc_cfg smqtt[2];     // 设计4个mqttClient 最大支持4个
    uint8_t ml307_mqttc_num;   // MQtt客户端ID号客户端数量(4)
    boker_regiser_sta ml307_reg_sta; // 自动注册状态
} ml307_cfg_t;

typedef struct {
    uint8_t ml_replay_buf[256];
    uint8_t ml_len;
    uint8_t mqttc_id;
} Mqtt_ML_data;

extern ml307_cfg_t ml307_cfg;

extern t_at_t *ML307R;

void set_http_register_enable(uint8_t enable);
uint8_t get_http_register_enable(void);
void set_http_register_status(uint8_t status);
uint8_t get_http_register_status(void);
uint16_t get_sptime(void);
uint8_t get_ml307_mqttc_number(void);
uint8_t set_sptime(uint16_t time);// 设置AT指令间隔时间
void set_ml307_mqtt_setup(uint8_t setup);
ml307_error_t ml307_send_cmd(char *CMD,                  //
                             uint16_t Data_len,           //
                             char *CMD_UCR,              //
                             t_at_urc_callback Phandle,  //
                             uint32_t waittime,          //
                             uint8_t resendCount         //
);
uint8_t ml_send_cmd(void *Ptx, uint8_t *dataBuf, uint16_t bufLen);														 
void ML307R_HW_SEND(t_at_send ml_send_cmd);

void ML307R_PUSH_DATA(uint8_t *dataBuf, uint16_t data_len);

ml307_error_t ml370_creat(ml307_cfg_t *ml307_cfg_t);



uint8_t get_ml307_mqttc_link(uint8_t mqttc_id);// 获取MQTT客户端连接状态

void set_ml307_mqttc_link(uint8_t mqttc_id, uint8_t link_status) ;// 设置MQTT客户端连接状态

ml307_error_t ml307_send_net_data(  t_at_t *ml,
                                    ml307_cfg_t *slm_cfg,
                                    uint8_t mqttc_id_t,
                                    t_at_urc_callback slm_send_callback,
                                    char *data,
                                    uint16_t datalen
                                    );
ml307_error_t ml_mqtt_modbus_urc_decode(    t_at_t *slm332,
                                            t_at_urc_callback urc_func,
                                            uint8_t *URC_info
                                            );

void ml_mqttc_sned_other_interface_1(char *data, uint16_t len);
void ml_mqttc_sned_other_interface_2(char *data, uint16_t len);
void ml_mqttc_sned_other_interface_3(char *data, uint16_t len);
void ml_mqttc_sned_other_interface_4(char *data, uint16_t len);

//提供直接修改结构体的API
mqttc_error_t ml307_set_mqttc_ssl(uint8_t mqttc_id, void *dev_info, uint8_t ssl_enable);
mqttc_error_t ml307_set_mqttc_topic_mux(uint8_t mqttc_id, void *dev_info, uint8_t topic_mux);
mqttc_error_t ml307_set_mqttc_client_id(uint8_t mqttc_id, void *dev_info, char *client_id);
mqttc_error_t ml307_set_mqttc_user_name(uint8_t mqttc_id, void *dev_info, char *User_name);
mqttc_error_t ml307_set_mqttc_password(uint8_t mqttc_id, void *dev_info, char *pass_word);
mqttc_error_t ml307_set_mqttc_host(uint8_t mqttc_id, void *dev_info, char *host);
mqttc_error_t ml307_set_mqttc_port(uint8_t mqttc_id, void *dev_info, int port);
mqttc_error_t ml307_set_mqttc_subscribe_topic(uint8_t mqttc_id, void *dev_info,char *mqttc_subTopic);
mqttc_error_t ml307_set_mqttc_publish_topic(uint8_t mqttc_id, void *dev_info,char *mqttc_pubTopic);

#ifdef __cplusplus
}
#endif
#endif

/*ML307_H*/